import time
import requests
import feedparser
from newspaper import Article

# 获取RSS内容
def fetch_feed(url, retries=3, delay=5):
    headers = {'User-Agent': 'Mozilla/5.0'}
    for attempt in range(retries):
        try:
            res = requests.get(url, headers=headers, timeout=10)
            res.raise_for_status()
            return feedparser.parse(res.content)
        except Exception as e:
            print(f"第 {attempt+1} 次尝试失败: {e}")
            time.sleep(delay)
    print(f"❌ 跳过源: {url}")
    return None

# 获取文章正文并缓存
article_cache = {}
def get_article_text(url):
    if url in article_cache:
        return article_cache[url]
    try:
        article = Article(url)
        article.download()
        article.parse()
        content = article.text[:1500]
        article_cache[url] = content
        return content
    except Exception:
        return "(内容获取失败)" 